package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.PayOrderDTO;
import com.cskaoyan.wordmemorize.dto.app.VocPaySaleDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.PaySuccessNotifyCommand;
import com.cskaoyan.wordmemorize.request.VocPaySaleCommand;
import com.cskaoyan.wordmemorize.service.UserPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user/pay")
public class UserPayController {
    @Autowired
    UserPayService userPayService;

    // 1.APP支付接口(解锁等级，购买代词上限, 购买补签卡)
    @PostMapping
    public Result pay(@RequestBody VocPaySaleCommand command) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        PayOrderDTO pay = userPayService.pay(command, userId);
        return Result.ok(pay);
    }

    // 2.支付成功接口，我需要我们自己通过postman或者Api Fox模拟支付成功的通知(解锁等级，购买代词上限, 购买补签卡)
    @PostMapping("notice")
    public Result paySuccess(@RequestBody PaySuccessNotifyCommand command) {

        String paySuccess = userPayService.paySuccess(command, command.getUserId());
        return Result.ok(paySuccess);
    }

    // 3.APP获取支付结果*
    @GetMapping("{paySaleId}")
    public Result queryTradeStatus(@PathVariable Long paySaleId) {
        Integer queryTradeStatus = userPayService.queryTradeStatus(paySaleId);
        return Result.ok(queryTradeStatus);
    }

    // 4.APP获取购买历史**
    @GetMapping("list")
    public Result getPaySaleHistory(PageRequest pageRequest) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<VocPaySaleDTO> paySaleHistory = userPayService.getPaySaleHistory(userId, pageRequest);
        return Result.ok(paySaleHistory);
    }
}
